<template>
    <div class="login_container">
        <div class="login_box">
            <div class="login_logo">
                <img src="../assets/logo.png">
            </div>
            <!-- 设置ref属性可以获取到对应的对象 不如这里就可以通过ref获取表单对象 -->
            <el-form ref="loginFormRef" :model="loginForm" label-width="0px" :rules="loginRules" class="login_form">
                <el-form-item prop="name">
                  <el-input v-model="loginForm.name" prefix-icon="iconfont iconyonghuming"></el-input>
                </el-form-item>

                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont iconmima"></el-input>
                </el-form-item>

                <el-form-item class="buts">
                   <el-button type="primary" @click="login">确定</el-button>
                    <el-button type="info" @click="reset">重置</el-button>
                </el-form-item>

            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        name: '111',
        password: '111'
      },
      loginRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async validate => {
        if (validate) {
          const { data: result } = await this.$http.post('/api/login', this.loginForm)
          console.log(result)
          if (result.message.status !== 200) {
            return this.$message.error('登陆失败!')
          }
          // 将token 保存到会话中
          window.sessionStorage.setItem('token', result.data.token)

          this.$message({
            message: '登陆成功',
            type: 'success'
          })
          this.$router.push('/home')
        }
      })
    },
    // 重置
    reset () {
      this.$refs.loginFormRef.resetFields()
    }
  }

}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}

.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position:absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

.login_logo{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position:absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    img{
        width:100%;
        height:100%;
         border-radius: 50%;
         background-color: #eee;
    }
}

.buts{
    display: flex;  //布局方式是弹性布局
    justify-content:flex-end;  //弹性盒子元素将向行结束位置对齐
}

.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box; //为元素设定的宽度和高度决定了元素的边框盒.就是说，为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制。
}
</style>
